package com.hall;

import com.hall.items.aggr.MyWeaponProperties;
import com.hall.items.repo.MyWeaponRepository;
import com.hall.weaponenhancement.aggr.GoldAccount;
import com.hall.weaponenhancement.aggr.PlayerGoldAccounts;
import com.sharecommon.auth.filter.TokenFilter;
import dml.accounting.repo.AccountRepository;
import erp.ERP;
import erp.mongodb.MongodbRepository;
import erp.repository.interfaceimplementer.InterfaceRepositoryImplBuilder;
import org.apache.kafka.clients.admin.NewTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.kafka.config.TopicBuilder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;

@SpringBootApplication
public class HallApplication extends SpringBootServletInitializer implements
        WebMvcConfigurer {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(HallApplication.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ProxyPlayerAuthInterceptor playerAuthInterceptor;

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
        return builder.sources(HallApplication.class);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(playerAuthInterceptor).addPathPatterns(
                "/items/**", "/we/**");
    }

    @Bean
    public FilterRegistrationBean<Filter> registTokenFilter() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new TokenFilter());
        registration.addUrlPatterns("/items/*", "/we/*");
        registration.setName("TokenFilter");
        registration.setOrder(1);
        return registration;
    }


    @Bean
    public AccountRepository<GoldAccount, Long> goldAccountRepository() {
        return InterfaceRepositoryImplBuilder.build(AccountRepository.class,
                new MongodbRepository<>(mongoTemplate, GoldAccount.class));
    }

    @Bean
    public MongodbRepository<PlayerGoldAccounts, Long> playerGoldAccountsRepository() {
        return new MongodbRepository<>(mongoTemplate,
                PlayerGoldAccounts.class);
    }

    @Bean
    public MyWeaponRepository weaponRepository() {
        return new MyWeaponRepository(mongoTemplate);
    }

    @Bean
    public MongodbRepository<MyWeaponProperties, Long> myWeaponPropertiesRepository() {
        return new MongodbRepository<>(mongoTemplate,
                MyWeaponProperties.class);
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("process")
                .partitions(10)
                .replicas(1)
                .build();
    }

    public static void main(String[] args) {
        ERP.useAnnotation();
        SpringApplication.run(HallApplication.class, args);
    }
}
